// import { element } from 'prop-types';
import { getNoticeList, fetchNoticeDetail, delNotice, addNotice, updateNotice } from '@/services/notice/notice';
import { getEnableOrgTree } from '@/services/Organization/orgmanage';

export default {
    namespace: 'noticeManage',
    state: {
        data: [],
        changeData: {
            id: null,
            content: null,
            recvCompany: '',
            startTime: null,
            title: ''
        },
        pagination: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        companyTree: []
    },
    effects: {
        *fetchList({ payload, callback }, { put, call }) {
            const result = yield call(getNoticeList, payload);
            if (result && result.code === 10000) {
                const reTag = /<img(?:.|\s)*?>/g;
                const newArr = result.data.rows;
                if (newArr.length > 0) {
                    newArr.forEach((item, index) => {
                        newArr[index].content = newArr[index].content.replace(reTag, '');
                    });
                }
                yield put({
                    type: 'save',
                    payload: {
                        data: newArr,
                        pagination: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
            if (callback) callback(result);
        },
        *fetchDetail({ payload, callback }, { put, call }) {
            const result = yield call(fetchNoticeDetail, payload);
            yield put({
                type: 'save',
                payload: {
                    changeData: result.data
                }
            });
            if (callback) callback(result);
        },
        *deleteNotice({ payload, callback }, { call }) {
            const result = yield call(delNotice, payload);
            if (callback) callback(result);
        },
        *addNotice({ payload, callback }, { put, call }) {
            const result = yield call(addNotice, payload);
            yield put({
                type: 'save',
                payload: {
                    changeData: result.data
                }
            });
            if (callback) callback(result);
        },
        *updateNotice({ payload, callback }, { put, call }) {
            const result = yield call(updateNotice, payload);
            yield put({
                type: 'save',
                payload: {
                    changeData: result.data
                }
            });
            if (callback) callback(result);
        },
        *fetchCompanyTree({ payload, callback }, { put, call }) {
            const result = yield call(getEnableOrgTree, payload);
            if (result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        companyTree: result.data
                    }
                });
            }
            if (callback) callback(result);
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        },
        setPagination(state, { pagination }) {
            return {
                ...state,
                pagination
            };
        }
    }
};
